<template>
  <el-row type="flex" justify="space-between" align="middle" style="height: 40px; width: 100%">
    <el-col>
      <!-- 名称应该变成 对应的节点中的name -->
      <span>{{ treeNode.name }}</span>
    </el-col>
    <el-col :span="4">
      <el-row type="flex" justify="end">
        <!-- 两个内容 -->
        <el-col>{{ treeNode.manager }}</el-col>
        <el-col>
          <!-- 下拉菜单 element -->
          <el-dropdown @command="operaDeparts">
            <span> 操作<i class="el-icon-arrow-down" /> </span>
            <!-- 下拉菜单 -->
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="edit">编辑部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="del">删除部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delDepartmentsApi } from '@/api/departments'
export default {
  name: 'TreeTools',

  props: {
    treeNode: {
      type: Object,
      required: true,
    },
    isRoot: {
      default: false,
      type: Boolean,
    },
  },
  data() {
    return {}
  },

  mounted() {},

  methods: {
    operaDeparts(type) {
      console.log('type:', type)
      if (type === 'del') {
        this.$confirm('确定要删除该部门, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        })
          .then(async () => {
            const res = await delDepartmentsApi(this.treeNode.id)
            console.log('REs:', res)
            this.$emit('delDepts')
            this.$message({
              type: 'success',
              message: '删除成功!',
            })
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除',
            })
          })
      } else if (type === 'add') {
        // 这里是子组件 触发自定义事件 addDepts
        this.$emit('addDepts', this.treeNode)
        console.log('add')
      } else {
        console.log('编辑')
      }
    },
  },
}
</script>

<style lang="scss" scoped></style>
